package com.ice_col.guesssongname.ui;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.ice_col.guesssongname.R;
import com.ice_col.guesssongname.util.Util;

/**
 * Created by Administrator on 2015/10/14 0014.
 */
public class SelectedWordsView extends LinearLayout {
    public static final String TAG="SelectedWordsView";

    private Context mContext;

    private Button[] mButton;

    private boolean[] state;
    private int[] memento;


    public  boolean getState(int index) {
        return state[index];
    }

    public void setState( int i,boolean state) {
        this.state[i] = state;
    }


    private SelectedWordClickListener mCallback;

    public interface SelectedWordClickListener{
        void onSelectedWordClick(View v);
    }

    public SelectedWordsView(Context context) {
        super(context);
        mContext=context;
    }

    public SelectedWordsView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext=context;

    }

    public SelectedWordsView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext=context;
    }


    public void initButton(int count){
        removeAllViews();

        mButton=new Button[count];
        state=new boolean[count];
        memento=new int[count];
        for(int i=0;i<count;i++) {
            View view = Util.getView(mContext, R.layout.linear_item);
            addView(view);
            Button button=(Button) view.findViewById(R.id.selectedword_item_button);
            mButton[i]=button;
            button.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    mCallback.onSelectedWordClick(v);
                }
            });
            state[i]=false;
            memento[i]=-1;
        }

    }

    public void registerWordClickListener(SelectedWordClickListener callback){
        this.mCallback=callback;
    }

    public Button getButton(int index) {
        if(index>=mButton.length){
            Log.e(TAG,"out of bound,check getButton");
            return null;
        }
        return mButton[index];
    }

    public Button[] getButtons() {
        return mButton;
    }

    public int getMemento(int index) {
        if(index>=memento.length){
            Log.e(TAG,"out of bound,check getMemento");
            return -1;
        }
        return memento[index];
    }

    public void setMemento(int index,int value){
        if(index>=memento.length){
            Log.e(TAG,"out of bound,check getMemento");
            return;
        }
        memento[index]=value;
    }

    public int[] getMemento() {
        return memento;
    }

    public int getUnusedButtonIndex(){
        for(int i=0;i<state.length;i++){
            if(state[i]==false){
                return i;
            }
        }
        return -1;//means all button has a word
    }

    public boolean isFinished(){
        return getUnusedButtonIndex() == -1;
    }


}
